package cn.skymesh.onvif.sample;

import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

import cn.skymesh.onvif.DiscoveryManager;
import cn.skymesh.onvif.OnvifManager;
import cn.skymesh.onvif.listeners.DiscoveryListener;
import cn.skymesh.onvif.listeners.OnvifDeviceInformationListener;
import cn.skymesh.onvif.listeners.OnvifMediaProfilesListener;
import cn.skymesh.onvif.listeners.OnvifMediaStreamURIListener;
import cn.skymesh.onvif.listeners.OnvifResponseListener;
import cn.skymesh.onvif.listeners.OnvifServicesListener;
import cn.skymesh.onvif.models.Device;
import cn.skymesh.onvif.models.OnvifDevice;
import cn.skymesh.onvif.models.OnvifDeviceInformation;
import cn.skymesh.onvif.models.OnvifMediaProfile;
import cn.skymesh.onvif.models.OnvifServices;
import cn.skymesh.onvif.responses.OnvifResponse;

import static cn.skymesh.onvif.DiscoveryMode.ONVIF;

public class MainActivity extends AppCompatActivity {

    final DiscoveryListener listener = new DiscoveryListener() {
        @Override
        public void onDiscoveryStarted() {
            System.out.println("Discovery started");
            mLock.acquire();
        }

        @Override
        public void onDevicesFound(List<Device> devices) {
            mLock.release();
//            manager.discover(listener);
            for (Device device : devices)
                System.out.println("Devices found: " + device.getHostName());

//            connect(devices.get(0).getHostName() + ":2020" , "admin", "123456");
        }
    };
    DiscoveryManager manager = new DiscoveryManager();


    WifiManager.MulticastLock mLock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }


    private void connect(String host, String username, String password) {
        OnvifManager onvifManager = new OnvifManager();
        onvifManager.setOnvifResponseListener(new OnvifResponseListener() {
            @Override
            public void onResponse(OnvifDevice onvifDevice, OnvifResponse response) {
                Log.d("ONVIF", "onResponse");
            }

            @Override
            public void onError(OnvifDevice onvifDevice, int errorCode, String errorMessage) {
                Log.d("ONVIF", "onError code -> " + errorCode + " message -> " + errorMessage);
            }
        });


        OnvifDevice device = new OnvifDevice(host, username, password);


        onvifManager.getServices(device, new OnvifServicesListener() {
            @Override
            public void onServicesReceived(OnvifDevice onvifDevice, OnvifServices paths) {
                Log.d("ONVIF", "onServicesReceived ");
                Log.d("ONVIF", "onServicesReceived " + paths.getDeviceInformationPath());
                Log.d("ONVIF", "onServicesReceived " + paths.getProfilesPath());
                Log.d("ONVIF", "onServicesReceived " + paths.getServicesPath());
                Log.d("ONVIF", "onServicesReceived " + paths.getStreamURIPath());
            }
        });

        onvifManager.getDeviceInformation(device, new OnvifDeviceInformationListener() {
            @Override
            public void onDeviceInformationReceived(OnvifDevice device,
                                                    OnvifDeviceInformation deviceInformation) {
                Log.d("ONVIF", "onDeviceInformationReceived " + deviceInformation.toString());
            }
        });

        onvifManager.getMediaProfiles(device, new OnvifMediaProfilesListener() {
            @Override
            public void onMediaProfilesReceived(OnvifDevice device,
                                                List<OnvifMediaProfile> mediaProfiles) {
                Log.d("ONVIF", "onMediaProfilesReceived");
                for (OnvifMediaProfile profile: mediaProfiles){
                    Log.d("ONVIF", "onMediaProfilesReceived " + profile.toString());
                }


                onvifManager.getMediaStreamURI(device, mediaProfiles.get(0), new OnvifMediaStreamURIListener() {
                    @Override
                    public void onMediaStreamURIReceived(OnvifDevice device,
                                                         OnvifMediaProfile profile, String uri) {
                        Log.d("ONVIF", "onMediaStreamURIReceived");
                        Log.d("ONVIF", "onMediaStreamURIReceived profile " + profile.toString());
                        Log.d("ONVIF", "onMediaStreamURIReceived uri " + uri);
                    }
                });


            }
        });


    }

    public void start(View view) {
        WifiManager wifi = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

        mLock = wifi.createMulticastLock("ONVIF");

        manager.setDiscoveryTimeout(1000);
        manager.discover(ONVIF,listener);
    }
}